package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.ICompanyService;
import com.mdd.admin.validate.company.CompanyCreateValidate;
import com.mdd.admin.validate.company.CompanySearchValidate;
import com.mdd.admin.validate.company.CompanyUpdateValidate;
import com.mdd.admin.vo.company.CompanyDetailVo;
import com.mdd.admin.vo.company.CompanyListedVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.Company;
import com.mdd.common.mapper.CompanyMapper;
import com.mdd.common.util.TimeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 商务合作实现类
 * @author ctx
 */
@Service
public class CompanyServiceImpl implements ICompanyService {
        
    @Resource
    CompanyMapper companyMapper;

    /**
     * 商务合作列表
     *
     * @author cxt
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<CompanyListedVo>
     */
    @Override
    public PageResult<CompanyListedVo> list(PageValidate pageValidate, CompanySearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<Company> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        companyMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "like:companyName@company_name:str",
            "like:realName@real_name:str",
            "like:mobile:str",
            "=:companyContent@company_content:str",
            "=:myAdvantage@my_advantage:str",
        });

        IPage<Company> iPage = companyMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<CompanyListedVo> list = new LinkedList<>();
        for(Company item : iPage.getRecords()) {
            CompanyListedVo vo = new CompanyListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime()));
            //vo.setUpdateTime(TimeUtils.timestampToDate(item.getUpdateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 商务合作详情
     *
     * @author cxt
     * @param id 主键参数
     * @return Company
     */
    @Override
    public CompanyDetailVo detail(Integer id) {
        Company model = companyMapper.selectOne(
                new QueryWrapper<Company>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        CompanyDetailVo vo = new CompanyDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 商务合作新增
     *
     * @author cxt
     * @param createValidate 参数
     */
    @Override
    public void add(CompanyCreateValidate createValidate) {
        Company model = new Company();
        model.setCompanyName(createValidate.getCompanyName());
        model.setRealName(createValidate.getRealName());
        model.setMobile(createValidate.getMobile());
        model.setCompanyContent(createValidate.getCompanyContent());
        model.setMyAdvantage(createValidate.getMyAdvantage());
        model.setStatus(createValidate.getStatus());
        model.setRealName(createValidate.getRemark());
        model.setCreateTime(System.currentTimeMillis() / 1000);
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        companyMapper.insert(model);
    }

    /**
     * 商务合作编辑
     *
     * @author cxt
     * @param updateValidate 参数
     */
    @Override
    public void edit(CompanyUpdateValidate updateValidate) {
        Company model = companyMapper.selectOne(
                new QueryWrapper<Company>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setCompanyName(updateValidate.getCompanyName());
        model.setRealName(updateValidate.getRealName());
        model.setMobile(updateValidate.getMobile());
        model.setCompanyContent(updateValidate.getCompanyContent());
        model.setMyAdvantage(updateValidate.getMyAdvantage());
        model.setStatus(updateValidate.getStatus());
        model.setRemark(updateValidate.getRemark());
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        companyMapper.updateById(model);
    }

    /**
     * 商务合作删除
     *
     * @author cxt
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        Company model = companyMapper.selectOne(
                new QueryWrapper<Company>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        companyMapper.delete(new QueryWrapper<Company>().eq("id", id));
    }

}
